package com.zishi.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zishi.model.pojo.User;
import com.zishi.mapper.UserMapper;
import com.zishi.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zishi.util.ConstUtil;
import com.zishi.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author TP
 * @since 2022-05-05 08:53:41
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;



    /**
     * 根据openId获取用户主键id
     */
    @Override
    public Integer getUserIdByOpenId(String openid) {
        return userMapper.getUserIdByOpenId(openid);
    }

    /**
     * 根据用户id更新头像
     */
    @Override
    public String updateUserFavicon(MultipartFile file,  String openid) {
        String filePath = FileUploadUtil.upload(file, openid);
        String fileWebPath = ConstUtil.WEBSITE_IMAGE_PATH + "/" + filePath;
//        String databaseFilePath = fileWebPath  + ConstUtil.SEPARATOR;

        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        User user = new User();
        user.setFavicon(fileWebPath);
        updateWrapper.eq("openid", openid);

        userMapper.update(user, updateWrapper);
        return fileWebPath;
    }
}
